<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
    <a href="#">链接1号</a>
    <a href="#">链接2号</a>
    <a href="#">链接3号</a>
    <a href="#">链接4号</a>
    <a href="#">链接5号</a>
    <a href="#">链接6号</a>
    </div>,
    <script>
        const aList = document.querySelectorAll('a')
        // const aList = document.getElementsByTagName('a')
        console.log(aList);
        
        aList.forEach(item => {
            console.log(item.innerHTML);
        })
        const arr = [1, 2, 3]
// 通过调用iterator，拿到迭代器对象
const iterator = arr[Symbol.iterator]()
console.log(iterator);

// 对迭代器对象执行next，就能逐个访问集合的成员
iterator.next()
iterator.next()
iterator.next()

let a ={
    name: 'zf',
    age: 18,
    sex: '男'
}
let b = a
a.name = 'zf2'

console.log(b.name);
// 迭代器生成函数：接收商品集合，返回迭代器
function createCartIterator(products) {
  let idx = 0; // 游标：闭包保存遍历进度
  const len = products.length;

  return {
    next() {
      const done = idx >= len;
      // 未结束时返回当前商品，结束时返回undefined
      const value = !done ? products[idx++] : undefined;
      return { done, value };
    }
  };
}

// 测试：购物车商品数据（若数据格式错误，运行时才会暴露问题）
const cartProducts = [
  { id: 1, name: "无线鼠标", price: 99 },
  { id: 2, name: "机械键盘", price: 399 },
  { id: 3, name: "笔记本支架" } // 漏传price，JS不会报错，运行时可能出问题
];

// 遍历购物车
const cartIterator = createCartIterator(cartProducts);
let current = cartIterator.next();
while (!current.done) {
  const product = current.value;
  // 若product无price，console会输出"价格：undefined元"
  console.log(`商品：${product.name}，ID：${product.id}，价格：${product.price}元`);
  current = cartIterator.next();
}
    </script>
</body>
</html>